package com.cdj.common.view.decoration;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;


public class HorizontalSpacesItemDecoration extends RecyclerView.ItemDecoration {

    private boolean padding;
    private int space;

    public HorizontalSpacesItemDecoration(int space) {
        this.space = space;
    }

    public HorizontalSpacesItemDecoration(int space, boolean padding) {
        this(space);
        this.padding = padding;
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.top = space;
        outRect.bottom = space;

        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildLayoutPosition(view) == 0) {
            outRect.left = padding ? space : 0;
            outRect.right = space;
        } else if (parent.getChildLayoutPosition(view) == parent.getAdapter().getItemCount() - 1) {
            outRect.left = 0;
            outRect.right = padding ? space : 0;
        }else {
            outRect.left = 0;
            outRect.right = space;
        }
    }
}